package org.sk.colorfulguild.ui.battle

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import org.sk.colorfulguild.databinding.FragmentBattleBinding

class BattleFragment : Fragment() {

    private lateinit var mBinding: FragmentBattleBinding
    private val battleViewModel: BattleViewModel by lazy { ViewModelProvider(requireActivity())[BattleViewModel::class.java] }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        mBinding = FragmentBattleBinding.inflate(inflater, container, false)
        initViewModel()
        mBinding.btnBattle.setOnClickListener {
            if (battleViewModel.battleEnd) {
                mBinding.tvBattleLog.text = ""
            }
            battleViewModel.battle()
        }
        return mBinding.root
    }

    fun initViewModel() {
        mBinding.lifecycleOwner = this
        mBinding.viewModel = battleViewModel
        battleViewModel.battleLog.observe(viewLifecycleOwner) {
            mBinding.tvBattleLog.text = mBinding.tvBattleLog.text.toString() + it + "\n"
        }
    }
}